/// https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
pub struct Solution;

impl Solution {
    pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
        let n = nums.len();
        if n < 2 {
            return n as i32;
        }

        let mut slow = 0;
        for fast in 1..n {
            if nums[slow] != nums[fast] {
                nums[slow + 1] = nums[fast];
                slow += 1;
            }
        }
        (slow + 1) as i32
    }

    pub fn dedup(nums: &mut Vec<i32>) -> i32 {
        nums.dedup();
        nums.len() as i32
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn assert<F>(f: F)
        where F: Fn(&mut Vec<i32>) -> i32 {
        let mut nums = vec![0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
        assert_eq!(f(&mut nums), 5);
        assert_eq!(nums[..5], [0, 1, 2, 3, 4]);
    }

    bench!(assert Solution::remove_duplicates);
    bench!(assert Solution::dedup);
}
